from django.db import models
from django.contrib.auth.models import AbstractUser

from meiduo.utils.data_model import DataModel
from areas.models import Areas


class User(AbstractUser):
    phone_number = models.CharField(max_length=11, unique=True, verbose_name="手机号")
    email_active = models.BooleanField(null=False,default=False,verbose_name="邮箱是否激活")
    address_default = models.ForeignKey("Addresses",null= True,blank=True,on_delete=models.SET_NULL,related_name="users",verbose_name="默认地址")
    class Meta:
        db_table = "tb_users"
        verbose_name = "用户"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.username


class Addresses(DataModel):
    """创建用户收货地址模型"""
    user = models.ForeignKey(User,on_delete=models.CASCADE,related_name="address",verbose_name="用户")
    title = models.CharField(max_length=40,verbose_name="地址别名")
    receiver = models.CharField(max_length=20,verbose_name="收货人")
    province = models.ForeignKey(Areas,on_delete=models.PROTECT,related_name='province_address',verbose_name="省份")
    city = models.ForeignKey(Areas,on_delete=models.PROTECT,related_name="city_address",verbose_name="城市")
    district = models.ForeignKey(Areas,on_delete=models.PROTECT,related_name="district_address",verbose_name="区")
    place = models.CharField(max_length=40,verbose_name="详细地址")
    mobile = models.CharField(max_length=11,verbose_name="手机号")
    tel = models.CharField(max_length=20,default='',null=True,blank=True,verbose_name="固定电话")
    email = models.CharField(max_length=30,default='',null=True,blank=True,verbose_name="邮箱")
    is_deleted = models.BooleanField(default=False,verbose_name="逻辑删除")

    class Meta:
        db_table = "tb_address"
        verbose_name = "收货地址"
        verbose_name_plural = verbose_name
        ordering = ["-update_time"]

    def __str__(self):
        return self.title

